//program to implement Steganography
#include<iostream.h>
#include<dir.h>
#include<conio.h>
#include<string.h>
#include <stdlib.h>
#include <stdio.h>

void stegano_hide();
void stegano_retreive();
void main()
{
int opt;
clrscr();
do
{
cout<<"1.HIDE FILE\n";
cout<<"2.RETREIVE FILE\n";
cout<<"3.EXIT\n";
cout<<"Enter your choice:\n";
cin>>opt;
 switch(opt)
 {
 case 1:stegano_hide();
	break;
 case 2:stegano_retreive();
	break;
 }
}
 while(opt!=3);
getch();
}

void stegano_hide()
{
FILE *ifp = NULL,*tfp = NULL;
char ifile[30],tfile[30];
int c;

cout<<"Enter the image file: ";
gets(ifile);
cout<<"Enter the text file: ";
gets(tfile);

ifp = fopen(ifile,"a+b");
if (ifp == NULL)
{
  cout<<"Image file not found\n";
  exit(1);
}

tfp = fopen(tfile,"r");
if (tfp == NULL)
{
	cout<<"Text File cannot be opened\n";
	exit(1);
}
if (feof(ifp))
{
	cout<<"EOF of Image file reached before hiding\n";
	return;
}
//fputs("z*$!",ifp);
fprintf(ifp,"\n^*$!\n");

while(1)
{
	if (feof(tfp))
		break;
	c = fgetc(tfp);
	if (c!= -1)
		fputc(c,ifp);
}
//fputc(-1,ifp);
cout<<"File Hiding Complete\n";
fclose(tfp);
fclose(ifp);
}

void stegano_retreive()
{
	FILE *ifp = NULL,*tfp = NULL;
	char ifile[30],tfile[30];
	int ch;
	char c,s[6];
	int text_present = 0,start = 0;


	cout<<"Enter the Image File Name\n";
	gets(ifile);
	cout<<"Enter the New Text File Name\n";
	gets(tfile);

	ifp = fopen(ifile,"rt");
	if (ifp == NULL)
	{
		cout<<"Image File cannot be opened\n";
		getch();
		exit(1);
	}
	tfp = fopen (tfile,"w");
	if (tfp == NULL)
	{
		cout<<"Text File cannot be opened\n";
		getch();
		exit(1);
	}

	while(1)
	{
		if (feof(ifp))
			break;
		fgets(s,5,ifp);
		if (!strcmp(s,"^*$!"))
			text_present = 1;
		else
			text_present = 0;

//		c = ch;
	   /*	if (c == '^')
		{
			c = fgetc(ifp);
		       //c = ch;
			if (c == '*')
			{
				c = fgetc(ifp);
			       //c = ch;
				if (c == '$')
				{
					c = fgetc(ifp);
				      //c = ch;
					if(c == '!')
					{
						c = fgetc(ifp);
					       //	 c = ch;
						 if (c == '\n')
						 {
							text_present = 1;
							start = 1;
						 }
						else
							text_present = 0;
					}
				}
			}
		}
		*/


		if (text_present)
		{
			if (start)
			{
				c = fgetc(ifp);
				start = 0;
			}


			if (c!=-1)
			{
			fputc(c,tfp);
			}
		}
	  }
	  if (!text_present)
	  {
		cout<<"Image file contains no hidden data\n";
	  }
	  fclose(ifp);
	  fclose(tfp);

}
